import Vue from 'vue'
import {checkStatus, loginRefresh,loginStatus, login} from "@/api/api";
import {USER_COOKIE} from "@/store/mutation-types";

const userInfo = {
    state: {
        qrimg: '',
        cookie: '',
        account: undefined,
        profile: undefined,
        test: '',
    },
    mutations: {
        SET_QRIMG: (state, qrimg) => {
            state.qrimg = qrimg
        },
        SET_COOKIE: (state, cookie) => {
            state.cookie = cookie
        },
        SET_ACCOUNT: (state, account) => {
            state.account = account
        },
        SET_PROFILE: (state, profile) => {
            state.profile = profile
        },
        SET_TEST: (state, test) => {
            state.test = test
        }
    },
    actions: {
        login({commit}, params) {
            return new Promise((resolve, reject) => {
                // 获取二维码
                login(params).then(res => {
                    commit('SET_QRIMG', res.qrimg)
                    resolve(res);
                }).catch(error => {
                    reject(error)
                });
            })

        },
        checkLoginStatus({commit}, key) { // 检查登录状态
            return new Promise((resolve, reject) => {
                checkStatus(key).then(statusRes => {
                    if (statusRes.code === 800) {
                        console.log('二维码已过期,请重新获取!')
                        resolve(statusRes);
                    }
                    if (statusRes.code === 802) { // 授权中
                        console.log('授权中')
                        resolve(statusRes);
                    }
                    if (statusRes.code === 803) { // 授权成功
                        console.log('授权成功')
                        // 这一步会返回cookie
                        // this.$message.success('授权登录成功!')
                        Vue.ls.set(USER_COOKIE, statusRes.cookie)
                        commit('SET_COOKIE', statusRes.cookie)
                        // this.getLoginStatus(commit, statusRes.cookie);
                        resolve(statusRes);
                    }
                }).catch(error => {
                    reject(error)
                });
            });
        },
        getLoginStatus({commit}, cookie) { // 获取登录状态
            return new Promise((resolve, reject) => {
                let params = {
                    'cookie': cookie
                }
                loginStatus(params).then(res => {
                    if (res.code == 200) {
                        commit('SET_ACCOUNT', res.account)
                        commit('SET_PROFILE', res.profile)
                    }
                }).catch(error => {
                    reject(error)
                })
            });
        },
        loginRefresh() { // 刷新登录状态
            return new Promise((resolve, reject) => {
                let params = {
                    'cookie': Vue.ls.get(USER_COOKIE)
                }
                loginRefresh(params).then(res => {
                    // if (res.)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        test({commit}) {
            commit('SET_TEST', '11111')

        }
    }
}

export default userInfo
